package com.ordersystem.service.impl;

import com.ordersystem.domain.Purview;
import com.ordersystem.mappers.PurviewMapper;
import com.ordersystem.service.PurviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : asus1020
 * @version : [v1.0]
 * @className : PurviewServiceImpl
 * @description : [描述说明该类的功能]
 * @createTime : [2022/3/25 16:02]
 * @updateUser : asus1020
 * @updateTime : [2022/3/25 16:02]
 * @updateRemark : [描述说明本次修改内容]
 */
@Service
public class PurviewServiceImpl implements PurviewService {
    @Autowired
    private PurviewMapper purviewMapper;
    @Override//查询所有菜单
    public List<Purview> getAllMenuList() {
        List<Purview> purviewOne= purviewMapper.findPurviewByLevel(1);
        for (Purview purview : purviewOne) {
            Integer lastPid = purview.getPid();
            List<Purview> purviewTwo = purviewMapper.findPurviewByLastPid(lastPid);
            purview.setChildren(purviewTwo);
        }
        return purviewOne;
    }

    @Override//查询所有权限列表
    public List<Purview> findAllPurviewList() {
        List<Purview> purviewOne= purviewMapper.findPurviewByLevel(1);//查询一级列表
        for (Purview purview : purviewOne) {
            Integer lastPidTwo = purview.getPid();
            List<Purview> purviewTwo = purviewMapper.findPurviewByLastPid(lastPidTwo);
            for (Purview purviewLevelTwo : purviewTwo) {
                Integer lastPidTheir = purviewLevelTwo.getPid();
                List<Purview> purviewTheir = purviewMapper.findPurviewByLastPid(lastPidTheir);
                purviewLevelTwo.setChildren(purviewTheir);
            }
            purview.setChildren(purviewTwo);
        }
        return purviewOne;
    }

    @Override//根据权限查询所有权限泪飙
    @Transactional
    public List<Purview> findAllPurviewByPids(String pids) {
        if (pids==null||pids.equals(""))return null;
        String[] pidsStr = pids.split(",");
        ArrayList<Integer> pidsList = new ArrayList<>();
        for (String pidStr : pidsStr) {
            pidsList.add(Integer.parseInt(pidStr));
        }
        List<Purview> purviewByOne =
                purviewMapper.findPurviewByPidAndLevelAndlastPid(0, 1, pidsList);//根据权限查询所有一级权限
        for (Purview purviewOne : purviewByOne) {
            List<Purview> purviewByTwo =
                    purviewMapper.findPurviewByPidAndLevelAndlastPid(purviewOne.getPid(), 2, pidsList);//查询所有二级权限
            for (Purview purviewTwo : purviewByTwo) {
                List<Purview> purviewByThree =
                        purviewMapper.findPurviewByPidAndLevelAndlastPid(purviewTwo.getPid(), 3, pidsList);//查询三级权限
                purviewTwo.setChildren(purviewByThree);
            }//二级权限整合三级权限
            purviewOne.setChildren(purviewByTwo);//一级权限整合二级权限

        }
        return purviewByOne;
    }
}
